package cn.wolfcode.shop;

import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.service.ICataLogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PropertyController {
    @Reference
    private IPropertyService propertyService;
    @Reference
    private ICataLogService cataLogService;

    @RequestMapping("/property")
    public String propertyPage(Model model){
        model.addAttribute("allCatalog", cataLogService.SelectAllCache());
        return "property/property";
    }

    @RequestMapping("/property/get/{catalogId}")
    public String propertyListPage(@PathVariable("catalogId") Long catalogId, Model model){
        model.addAttribute("propertyList", propertyService.listByCatalogId(catalogId));
        return "property/property_list";
    }

    @RequestMapping("/property/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(Property property){
        JSONResultVo vo = new JSONResultVo();
        try {
            propertyService.saveOrUpdate(property);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

    @RequestMapping("/property/delete/{id}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("id") Long id){
        JSONResultVo vo = new JSONResultVo();
        try {
            propertyService.delete(id);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

}
